package servlet;

import entity.BookManage;
import service.BookManageService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/BookManageAdd")
public class BookManageAdd extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("test/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        BookManage bookManage = new BookManage();
        bookManage.setName(req.getParameter("name"));
        bookManage.setAuthor(req.getParameter("author"));
        bookManage.setTime(req.getParameter("time"));
        bookManage.setType(Integer.parseInt(req.getParameter("type")));

        if(new BookManageService().bookAdd(bookManage)>0){
            req.getSession().setAttribute("add",true);
        }else{
            req.getSession().setAttribute("add",false);
        }
        resp.sendRedirect("BookManageList");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
